<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  barData: {
    type: Object,
    default: () => ({})
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    let tempSeriesData = props?.barData?.barData?.map((x, i) => {
      return {
        data: x,
        type: 'bar',
        barWidth: pxToRem(100 * props?.dialogData?.fontSizeTimes),
        stack: '总比例',
        name: props?.barData?.legendData?.[i],
        label: {
          show: true,
          color: 'black',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          formatter: (val) => {
            return val.data ? `${val.data}%` : ''
          }
        }
      }
    })
    let option = {
      legend: {
        icon: 'rect',
        x: 'center',
        y: `${props?.dialogData?.fontSizeTimes === 1 ? 77 : 80}%`,
        width: '60%',
        itemWidth: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        itemHeight: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        textStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes)
        },
        data: props?.barData?.legendData
      },
      grid: {
        left: '0',
        right: '0%',
        bottom: '25%',
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 15 : 5}%`,
        containLabel: true
      },
      tooltip: {
        trigger: 'axis',
        valueFormatter: (val) => `${val}%`
      },
      dataZoom: { // 数据过多内部拖动逻辑
        type: "inside",
        zoomLock: true, // 不可缩放
        startValue: 0,
        endValue: 5,
        height: pxToRem(20),
        bottom: pxToRem(10)
      },
      xAxis: {
        type: 'category',
        data: props?.barData?.xAxisData,
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter',
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false, // 不显示坐标轴线
        },
        splitLine: {
          show: false // 不显示网格线
        },
      },
      yAxis: [
        {
          min: 0,
          max: 100,
          name: props?.barData?.yAxisLeftLabel,
          nameTextStyle: {
            color: 'white',
            fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
            padding: [0, 0, 0, pxToRem(-50 * props?.dialogData?.fontSizeTimes)]
          },
          axisTick: {
            show: false // 不显示坐标轴刻度线
          },
          axisLine: {
            show: false, // 不显示坐标轴线
          },
          splitLine: {
            show: true // 不显示网格线
          },
          axisLabel: {
            fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
            color: 'white',
            formatter: (val) => {
              return `${val}%`
            }
          }
        }
      ],
      series: tempSeriesData
    }
    option && myChart.value.setOption(option, true);

    myChart.value.on('legendselectChanged', (params) =>{
      event.stopPropagation();
    })
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};

defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h266 {
  height: 266px;
}
.dialog-h266 {
  width: 1120px;
  height: 500px;
}
</style>